package com.fangcun.controller.api.old;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.api.user.request.UserWithdrawalRequest;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseController;
import com.fangcun.entity.UserWithdrawCardNumber;
import com.fangcun.service.api.UserWithdrawaService;

@RestController
@RequestMapping("/api/{version}/auth/user/withdraw")
public class UserWithdrawController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(UserWithdrawController.class);

	@Autowired
	private UserWithdrawaService withdrawalService;

	// 添加银行卡
	@ApiVersion(1.0)
	@RequestMapping("add")
	private ResponseMsg add(@RequestBody UserWithdrawCardNumber request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			withdrawalService.add(request);
		} catch (ProException e) {
			LOG.error("-增加提现方式出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-增加提现方式异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	//
	@ApiVersion(1.0)
	@RequestMapping("list")
	private ResponseMsg list(@RequestBody PageBean<UserWithdrawCardNumber> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new UserWithdrawCardNumber());
			}
			request.getParam().setUserId(getApiUserId());
			msg.setData(withdrawalService.list(request));
		} catch (ProException e) {
			LOG.error("-查看账号列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-查看账号列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	// 提现
	@ApiVersion(1.0)
	@RequestMapping("record/add")
	private ResponseMsg withdraw(@RequestBody UserWithdrawalRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			withdrawalService.addWithdrawal(request);
		} catch (ProException e) {
			LOG.error("-已提交提现申请出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-已提交提现申请异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
